<?php

namespace App\Console\Commands\Script;

use App\Models\Common\Common;
use App\Models\Tools\Data;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SendWechatMessage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:send_wechat_message';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '发送微信推送通知';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 拒回推送
        $shops = DB::table('shop')
            ->leftJoin('lottery_users as lu', 'shop.we_id', '=', 'lu.we_id')
            ->where('shop.verified', '=', Data::DATA_DB_LAIFEI_SHOP_VERIFIED_FAILED)
            ->select('lu.openid', 'shop.shop_name', 'shop.verify_remark')
            ->get()
            ->toArray();
        $shops = json_decode(json_encode($shops), true);
        $this->comment('认证拒回共' . count($shops) . '条，即将开始推送。。');
        $bar = $this->output->createProgressBar(count($shops));
        $bar->start();
        $success = 0;
        $failed = 0;
        foreach ($shops as $shop) {
            $data = [
                'first' => '来飞网对您的店铺信息进行了审核，您需要对店铺信息进行一些修改。',
                'keyword1' => '已拒回',
                'keyword2' => [
                    'value' => $shop['verify_remark'],
                    'color' => '#F00'
                ],
                'remark' => '点击打开当前页面，按照失败原因修改提交店铺【' . $shop['shop_name'] . '】的信息，我们将尽快为您再次审核。',
            ];
            $result = Common::sendLaifeiMessage($shop['openid'], 'certify_fail', $data, '/shop');
            $result ? $success++ : $failed++;
            $bar->advance();
        }
        $bar->finish();
        $this->info("\n拒回推送完成，推送成功" . $success . "条，失败" . $failed . "条\n");


        // 通过推送
        $shops = DB::table('shop')
            ->leftJoin('lottery_users as lu', 'shop.we_id', '=', 'lu.we_id')
            ->where('shop.verified', '=', Data::DATA_DB_LAIFEI_SHOP_VERIFIED_SUCCESS)
            ->whereNotNull('shop.verify_remark')
            ->where('shop.verify_remark','<>','')
            ->select('lu.openid', 'shop.shop_name', 'shop.verify_remark')
            ->get()
            ->toArray();
        $shops = json_decode(json_encode($shops), true);
        $this->comment('认证通过且添加了备注信息的店铺共' . count($shops) . '条，即将开始推送。。');
        $bar = $this->output->createProgressBar(count($shops));
        $bar->start();
        $success = 0;
        $failed = 0;
        foreach ($shops as $shop) {
            $data = [
                'first' => '来飞网对您的店铺信息进行了审核，您虽通过了审核，但为提高您后续的使用体验，建议您对店铺信息进行一些修改。',
                'keyword1' => '店铺【'.$shop['shop_name'].'】',
                'keyword2' => date('Y-m-d H:i:s'),
                'remark' => '点击打开店铺编辑页面，按照提示修改并提交店铺【' . $shop['shop_name'] . '】的信息，我们将尽快为您再次审核。',
            ];
            $result = Common::sendLaifeiMessage($shop['openid'], 'certified', $data, '/shop');
            $result ? $success++ : $failed++;
            $bar->advance();
        }
        $bar->finish();
        $this->info("\n推送完成，本次推送成功" . $success . "条，失败" . $failed . "条\n");

        return true;
    }
}
